using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Xml;

namespace Reminder
{
    public class RemindersDocument
    {
        private List<Event> _events = new List<Event>();
        private bool _dirty;
        private string _path;

        public RemindersDocument() { }

        public RemindersDocument(string path)
        {
            _path = path;
        }

        public void AddEvent(Event evt)
        {   
            _events.Add(evt);
            _dirty = true;
            Save();
        }

        public void RemoveEvent(Event evt)
        {

        }

        public List<Event> Events
        {
            get { return _events; }
        }

        public void SetDirty(bool dirty)
        {
            _dirty = dirty;
        }

        public void SetDirty()
        {
            _dirty = true;
        }

        public string Path
        {
            get { return _path; }
            set { _path = value; }
        }

        private void Save(string filename)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(RemindersDocument));
            XmlTextWriter writer = new XmlTextWriter(filename, null);
            writer.Formatting = Formatting.Indented;
            serializer.Serialize(writer, this);
            writer.Close();
        }

        public void Save()
        {
            if (_dirty)
            {
                Save(_path);
            }
        }

        static public RemindersDocument Open(string filename)
        {
            XmlTextReader reader = new XmlTextReader(filename);
            XmlSerializer serializer = new XmlSerializer(typeof(RemindersDocument));
            RemindersDocument doc = (RemindersDocument)serializer.Deserialize(reader);
            reader.Close();
            if (doc != null)
            {
                doc.Path = filename;
                doc.SetDirty(false);
            }
            return doc;
        }
    }
}
